package com.wan.activity

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.text.Editable
import android.util.Log
import com.wan.R
import com.wan.base.BaseBean
import com.wan.base.WanApp
import com.wan.entity.LoginEntity
import com.wan.uitls.ApiManager
import com.wan.uitls.CommonUtils
import com.wan.uitls.RetrofitHelper
import kotlinx.android.synthetic.main.activity_login.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.lang.StringBuilder


/**
 * 登录
 */
class LoginActivity : AppCompatActivity() {
    private val mThreadName = this.javaClass.name



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        login_account_tit.setText("147258369")
        login_pass_tit.setText("123456")

        login_login_btn.setOnClickListener {
            loginMethod()
        }

        login_register_tv.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
            finish()
        }
    }

    private fun loginMethod() {
        val loginAccount = login_account_tit.text.toString()
        if (loginAccount.isBlank()) {
            CommonUtils.toastWord(this, "登陆账号为空")
            return
        }
        val loginPass = login_pass_tit.text.toString()
        if (loginPass.isBlank()) {
            CommonUtils.toastWord(this, "登陆密码为空")
            return
        }

        val manager = ApiManager(this)
        manager.postLoginData(loginAccount, loginPass)
                .enqueue(object : Callback<BaseBean<LoginEntity>> {
                    override fun onResponse(call: Call<BaseBean<LoginEntity>>, response: Response<BaseBean<LoginEntity>>) {
                        Log.d(mThreadName, "onResponse : 登陆数据:==> $response")
                        Log.d(mThreadName, "onResponse : 请求cookie${response.headers()}")
                        val entity = response.body()!!.data

                        val cookie: StringBuilder = StringBuilder()
                        response.headers().values("Set-Cookie").forEach {

                            Log.d(mThreadName, "onResponse : 单个cookie ==> $it")
                            cookie.append(it).append(";")
                        }
                        var realCookie = cookie.deleteCharAt(cookie.lastIndex).toString()
                        Log.d(mThreadName, " onResponse : 存储的cookie ==> $realCookie")
                        entity.token = realCookie
                        entity.password = loginPass
                        
                        CommonUtils.saveLoginMessage(this@LoginActivity, entity)
                        CommonUtils.saveLoginState(this@LoginActivity,true)

                        CommonUtils.toastWord(WanApp.sContext, "登陆成功")
                        finish()
                    }

                    override fun onFailure(call: Call<BaseBean<LoginEntity>>, t: Throwable) {
                        Log.e(mThreadName, "onFailure : 登陆数据失败 ==> ${call.execute().body()}", t)
                    }

                })
        RetrofitHelper.get().getAllCall()

    }

    override fun onDestroy() {
        super.onDestroy()
        RetrofitHelper.get().getAllCall()
        RetrofitHelper.get().cancelCall(this)
    }

}
